﻿using Infragistics.Controls.Menus;
using Sidvall.UI.Dialogs;
using Sidvall.UI.Entities;
using Sidvall.UI.Wpf.ViewModels;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;

namespace Sidvall.UI.Wpf.Dialogs
{
    public partial class XamFileDialog
    {
        #region Public Members

        #region ViewModel

        public FileDialogViewModel ViewModel
        {
            get
            {
                return this.DataContext as FileDialogViewModel;
            }
            set
            {
                this.DataContext = value;
            }
        }

        #endregion

        #region ShowAsync

        public async Task ShowAsync(Sidvall.IO.IStorage storage, string rootPath, OpenFileDialogContext context, object owner)
        {
            FileDialogViewModel item;

            if (context == null)
                throw new System.ArgumentNullException("context");

            item = new FileDialogViewModel()
            {
                Title = "Open File",
                Folders = new TreeItemCollection(),
                RootPath = rootPath,
                Context = context,
            };
            if (storage != null)
                item.StorageManager = new Sidvall.IO.StorageManager(storage);
            else
                item.StorageManager = Sidvall.SystemContext.Current.SystemStorageManager;
            await item.LoadAsync(item.Folders);
            this.ViewModel = item;
            WindowManager.AddControlToWindow(this, owner);
            await WaitForClose();
        }

        #endregion

        #endregion
        #region Events

        #region XamDataTree_NodeExpansionChanged

        private async void XamDataTree_NodeExpansionChanged(object sender, Infragistics.Controls.Menus.NodeExpansionChangedEventArgs e)
        {
            XamDataTreeNode node;

            node = e.Node;
            if (node != null && node.IsExpanded)
            {
                await this.ViewModel.LoadAsync(node.Data as TreeItem, false, null);
            }
        }

        #endregion
        #region XamDataTree_SelectedNodesCollectionChanged

        private async void XamDataTree_SelectedNodesCollectionChanged(object sender, NodeSelectionEventArgs e)
        {
            XamDataTreeNode node;

            if (e.CurrentSelectedNodes != null && e.CurrentSelectedNodes.Count > 0)
            {
                node = e.CurrentSelectedNodes[0];
                await this.ViewModel.LoadAsync(node.Data as TreeItem, true, null);
            }
        }

        #endregion
        #region XamGrid_MouseDoubleClick

        private async void XamGrid_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (await this.ViewModel.SelectActiveFolderItemAsync())
                CloseDialog();
        }

        #endregion
        #region XamDialogWindow_WindowStateChanged

        private void XamDialogWindow_WindowStateChanged(object sender, Infragistics.Controls.Interactions.WindowStateChangedEventArgs e)
        {
            if (e.NewWindowState == Infragistics.Controls.Interactions.WindowState.Hidden)
            {
                this.ViewModel.Cancel();
                CloseDialog();
            }
        }

        #endregion
        #region OnCancelClick

        private void OnCancelClick(object sender, RoutedEventArgs e)
        {
            this.ViewModel.Cancel();
            CloseDialog();
        }

        #endregion
        #region OnOpenClick

        private async void OnOpenClick(object sender, RoutedEventArgs e)
        {
            if (await this.ViewModel.OpenAsync())
                CloseDialog();
        }

        #endregion

        #endregion
        #region Private Members

        ManualResetEvent myManualEvent = new ManualResetEvent(false);

        #region CloseDialog

        private void CloseDialog()
        {
            if (this.ViewModel != null)
                this.ViewModel.Dispose();
            Sidvall.UI.Wpf.WindowManager.RemoveControlFromWindow(this, this.Parent);
            myManualEvent.Set();
        }

        #endregion
        #region WaitForClose

        private async Task WaitForClose()
        {
            await Task.Run((System.Action)WaitForCloseAction);
        }
        private void WaitForCloseAction()
        {
            myManualEvent.WaitOne();
        }

        #endregion

        #endregion
        #region Constructors

        public XamFileDialog()
        {
            InitializeComponent();

            Sidvall.UI.Wpf.ControlManager.SetLanguage(this);
        }

        #endregion
    }
}
